package summer.student.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import summer.student.Dao.CollegeDictionaryDao;
import summer.student.Domain.CollegeDictionary;
import summer.student.Utils.Result;

import java.util.List;
import java.util.Map;

/**
 * @program: student
 * @description:
 * @author: hlt
 * @create: 2023-07-14 17:25
 **/
@RestController
@RequestMapping("/college")
@CrossOrigin(origins = "*", maxAge = 3600)  // 后端解决跨域
@Api(tags = "学院管理相关接口")
public class CollegeDictionaryController {

    @Autowired
    private CollegeDictionaryDao collegeDictionaryDao;

    @GetMapping("/getCollegeList")
    public Result getCollegeList(@RequestParam(name = "currentPage", defaultValue = "1", required = false) int currentPage,
                                 @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize,
                                 @RequestParam(name="collegeName", required = false) String collegeName){
        LambdaQueryWrapper<CollegeDictionary> clqw = new LambdaQueryWrapper<>();
        Page<CollegeDictionary> collegeDictionaryPage = new Page<>(currentPage, pageSize);
        if (collegeName != null)
            clqw.like(CollegeDictionary::getCollegeName, collegeName);
        Page<CollegeDictionary> page = collegeDictionaryDao.selectPage(collegeDictionaryPage, clqw);
        return Result.Success(page);

    }

    @PostMapping("/addCollege")
    public Result addCollege(@RequestBody Map<String, String>data){
        String collegeName = data.get("collegeName");
        CollegeDictionary college = new CollegeDictionary(null, collegeName);
        collegeDictionaryDao.insert(college);
        return Result.Success("增加学院成功！");
    }
}
